package com.dashboard.cloud.common.module;

import lombok.Data;

import java.io.Serializable;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: spring-cloud-dashboard
 * @Package: com.dashboard.cloud.common.module
 * @ClassName: APIResult
 * @Description: [接口返回结果]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/11/8 14:44
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Data
public class APIResult<T> implements Serializable {
    private final static int SUCCESS_CODE = 200;
    private final static int FAILURE_CODE = 500;

    private static final long serialVersionUID = -3760407144012227236L;
    /**
     * 响应编码：200-成功，500-失败
     */
    private Integer code;
    /**
     * 响应状态：true-成功，false-失败
     */
    private Boolean status;
    /**
     * 响应提示
     */
    private String mesg;
    /**
     * 响应内容
     */
    private T data;

    // -----------------------------------------------------------------------------------------------------------------
    public static <T> APIResult<T> success() {
        return newInstance(true, "成功", null);
    }

    public static <T> APIResult<T> success(T data) {
        return newInstance(true, "成功", data);
    }

    public static <T> APIResult<T> success(String message, T data) {
        return newInstance(true, message, data);
    }

    public static <T> APIResult<T> failure() {
        return newInstance(false, "失败", null);
    }

    public static <T> APIResult<T> failure(String message) {
        return newInstance(false, message, null);
    }

    public static <T> APIResult<T> newInstance(Boolean status, String message, T data) {
        APIResult<T> result = new APIResult<>();
        result.setCode(status ? SUCCESS_CODE : FAILURE_CODE);
        result.setStatus(status);
        result.setMesg(message);
        result.setData(data);
        return result;
    }
}